import 'package:flutter/material.dart';
import 'package:lite_message/style/lm_font_style.dart';
import 'package:lite_message/style/lm_icons.dart';
import 'package:lite_message/util/dio_util.dart';
import 'package:lite_message/widgets/count_down_btn.dart';
import 'package:lite_message/widgets/custom_bg.dart';
import 'package:lite_message/widgets/lm_button.dart';
import 'package:lite_message/widgets/lm_text_field.dart';
import 'package:lite_message/widgets/logo.dart';
import 'package:lite_message/util/app_util.dart';

///找回密码
class ForgetPsw extends StatelessWidget {
  const ForgetPsw({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomBg(
      body: ListView(
        physics: LM.LmPhysics,
        padding: EdgeInsets.symmetric(horizontal: 90.px),
        children: [
          SizedBox(height: 80.px),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              const Logo(sizeType: 2),
              SizedBox(width: 40.px),
              Text('忘记密码', style: LMFontStyle.title2(context)),
            ],
          ),
          SizedBox(height: 100.px),
          const ForgetPswBody(),
        ],
      ),
    );
  }
}

///输入框主体
class ForgetPswBody extends StatefulWidget {
  const ForgetPswBody({Key key}) : super(key: key);

  @override
  _ForgetPswBodyState createState() => _ForgetPswBodyState();
}

class _ForgetPswBodyState extends State<ForgetPswBody> {
  Map _info;

  @override
  void initState() {
    super.initState();
    _info = Map<String, String>();
    _info['email'] = '';
    _info['code'] = '';
    _info['password'] = '';
    _info['confirmPassword'] = '';
  }

  ///获取验证码
  Future<bool> _getCode() async {
    final bool _s = await DioUtil.getCode(context, _info['email']);
    LM.log(_s ? '发送成功' : '发送失败');

    return _s;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        LMTextField(
          initialValue: _info['email'],
          hintText: '邮箱',
          keyboardType: TextInputType.emailAddress,
          icon: LmIcons.email,
          onChanged: (v) {
            _info['email'] = v;
            if (v.length == 0 || v.length == 1) {
              setState(() {});
            }
          },
        ),
        SizedBox(height: 60.px),
        LMTextField(
          hintText: '验证码',
          keyboardType: TextInputType.number,
          icon: LmIcons.email,
          onChanged: (v) => _info['code'] = v,
          suffixIcon: CountDownBtn(
            onTap: _info['email'].length == 0
                ? null
                : () async {
                    return await _getCode();
                  },
          ),
        ),
        SizedBox(height: 60.px),
        LMTextField(
          maxLength: 16,
          hintText: '新密码',
          obscureText: true,
          keyboardType: TextInputType.text,
          icon: LmIcons.lock,
          onChanged: (v) => _info['password'] = v,
        ),
        SizedBox(height: 60.px),
        LMTextField(
          maxLength: 16,
          obscureText: true,
          hintText: '重复新密码',
          keyboardType: TextInputType.text,
          icon: LmIcons.lock,
          onChanged: (v) => _info['confirmPassword'] = v,
        ),
        SizedBox(height: 90.px),
        LmButton(
          text: '提交',
          onPressed: () {},
        ),
      ],
    );
  }
}
